package com.ts.api.module.api.entity.vo;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.IteratorUtils;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.base.StringUtils;
import com.gitee.apanlh.util.reflection.ClassConvertUtils;
import com.gitee.apanlh.util.reflection.ClassTypeUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.common.constant.api.ApiServiceCodeEnum;
import com.ts.api.module.api.entity.ApiUserRequest;
import com.ts.api.module.api.entity.po.ApiKeyPO;
import lombok.Data;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 *  请求参数实体
 *
 *  @author Pan
 */
@Data
public class ReqApiParameterVO {

    /** 渠道实体 */
    private ApiKeyPO apiKeyPO;
    /** 解析过的API用户对象 */
    private ApiUserRequest apiUserRequest;
    /** 用户业务表单 */
    private Map<String, String> formMap;
    /** API业务CODE */
    private ApiServiceCodeEnum serviceCode;

    public ReqApiParameterVO() {
        super();
        this.formMap = MapUtils.newLinkedHashMap();
    }

    /**
     *  构建请求API实例
     *
     *  @param  apiUserRequest
     *  @param  apiKeyPO
     *  @return ReqApiParameterVO
     */
    public static ReqApiParameterVO create(ApiUserRequest apiUserRequest, ApiKeyPO apiKeyPO) {
        ReqApiParameterVO reqApiParameterVO = new ReqApiParameterVO();
        reqApiParameterVO.setApiKeyPO(apiKeyPO);

        //  设置小写phoneMd5
        apiUserRequest.setPhoneMd5(StringUtils.lowerStr(apiUserRequest.getPhoneMd5()));
        reqApiParameterVO.setApiUserRequest(apiUserRequest);

        //  将ApiUserInfo转化map-转化StringMap
        Map<String, Object> map = ClassConvertUtils.toLinkedHashMap(apiUserRequest);
        Map<String, String> strMap = MapUtils.newLinkedHashMap(newMap ->
            IteratorUtils.entrySet(map, (k, v) -> {
                if (ClassTypeUtils.isString(v)) {
                    String newValue = (String) v;
                    if (ValidParam.isNotEmpty(newValue)) {
                        newMap.put(k, newValue);
                    }
                }
            })
        );

        reqApiParameterVO.setFormMap(strMap);
        return reqApiParameterVO;
    }

    /**
     *  构建请求对象，根据phoneMd5
     *
     *  @param  phoneMd5
     *  @return ReqApiParameterVO
     */
    public static ReqApiParameterVO createByPhoneMd5(String phoneMd5) {
        ReqApiParameterVO reqApiParameterVO = new ReqApiParameterVO();
        reqApiParameterVO.setFormMap(MapUtils.newHashMap(newMap -> newMap.put("phoneMd5", phoneMd5)));
        return reqApiParameterVO;
    }

    /**
     *  根据请求体构建
     *
     *  @param  body      请求实体
     *  @param  apiKeyPO  api客户端
     *  @return ReqApiParameterVO
     */
    public static ReqApiParameterVO createByRequestBody(String body, ApiKeyPO apiKeyPO) {
        ReqApiParameterVO reqApiParameterVO = new ReqApiParameterVO();
        reqApiParameterVO.setApiKeyPO(apiKeyPO);

        //  设置请求参数
        Map<String, String> formMap = reqApiParameterVO.getFormMap();
        JSONObject jsonObject = JSON.parseObject(body);
        LinkedHashMap<?, ?> to = jsonObject.to(LinkedHashMap.class);
        IteratorUtils.entrySet(to, (k , v) -> {
            String key = k.toString();
            String value = ClassConvertUtils.toStr(v);

            //  避免大写发生
            if (Eq.str("phoneMd5", key)) {
                formMap.put(key, StringUtils.lowerStr(value));
                return ;
            }
            formMap.put(key, value);
        });

        //  转换ApiUserInfo参数
        ApiUserRequest apiUserRequest = ClassConvertUtils.toBean(formMap, ApiUserRequest.class);
        reqApiParameterVO.setApiUserRequest(apiUserRequest);
        return reqApiParameterVO;
    }
}
